// pages/shop/orders.js
var app = getApp();
var curPage = 1;
Page({
  data: {
    orders: [],
    more: true,
    navbar: [{
      status: 0,
      name: '全部'
    }, {
      status: 2,
      name: '已支付'
    }, {
      status: 4,
      name: '已发货'
    }, {
      status: 6,
      name: '售后中'
    }, {
      status: 7,
      name: '已完成'
    }],
    navbarIndex: 0
  },
  onLoad: function (options) {
    this.setData({
      windowHeight: app.systemInfo.windowHeight
    })
  },
  onShow: function () {
    var that = this;

    var curStatus = that.data.navbar[that.data.navbarIndex].status;
    this.getOrders(1, curStatus);
    curPage = 1;
  },
  getOrders: function (page, status) {
    var that = this;
    wx.showLoading({
      title: '订单加载中...',
      mask: true
    });
    var req = {
      page: page,
      status: status,
      is_business: true
    }
    if (status == 0) {
      req = {
        page: page,
        is_business: true
      };
    }
    app.func.req('orders/index', req, 'GET', function (res) {
      var orders = res.result;
      for (var i = 0; i < orders.length; i++) {
        orders[i].status_alias = app.util.orderStatus(orders[i].status)
      }
      var curOrders = orders;
      if (page > 1) {
        curOrders = that.data.orders.concat(orders);
      }
      that.setData({
        orders: curOrders
      })
      if (res.result.length == 0) {
        that.setData({
          more: false
        })
      }
      wx.hideLoading();
    }, {
        token: wx.getStorageSync('token')
      })
  },
  selectStatus: function (e) {
    var that = this;
    var index = e.target.dataset.index;
    var ordersAll = this.data.ordersAll;
    this.setData({
      navbarIndex: index,
      more: true,
      scrollTop: 0
    })

    var curStatus = that.data.navbar[index].status;
    this.getOrders(1, curStatus);
    curPage = 1;
  },
  loadMore: function () {
    var that = this;
    var curStatus = that.data.navbar[that.data.navbarIndex].status;
    var more = this.data.more;
    curPage++;
    if (more) {
      this.getOrders(curPage, curStatus);
    }
  }
})
